define(function(require){

    require('./hongbao-detail-tpl');

    var Tpl = juicer.templates['hongbao-detail.juicer.html'];


    var hongbaoDetail = {

        detailEntry : {},

        load : false,

        init : function(orderId,mobile,orderMobile) {
            this.detailEntry.orderId = orderId;
            this.detailEntry.mobile = mobile;
            this.detailEntry.orderMobile = orderMobile;
            this.build();
            this.bindEvent();
            this.initValidator();
            this.initDatetimepicker();
            this.formatStyle($('#vality-time').val());
            this.dateStyle();
        },
        bindEvent : function() {
            var self = this;
            $("#reason_select").on('change', function () {
                if($(this).val() == "其它"){
                    $("#reason").val("");
                }else{
                    $("#reason").val($(this).val());
                }
            });
            $("#sendHongbao_btn").on('click', function () {
                self.doSendHongbao();
            });
        },
        /**
         * 界面渲染
         */
        build : function() {
            CRM.showModal(Tpl({
                "data" : this.detailEntry
            }), $('body'));
        },
        /**
         * 选择时间类型
         */
        dateStyle: function () {
            $('#dynamic-date').on('click',function(){
                $('.data-change').css({"display":"none"});
                $('#vality').css({"display":"block"});
                $('#startTime_view').val("");
                $('#startTime').val("");
                $('#expirationTime_view').val("");
                $('#expirationTime').val("");
                hongbaoDetail.formatStyle($('#vality-time').val());
            });
            $('#defined-date').on('click',function(){
                $('.data-change').css({"display":"block"});
                $('#vality').css({"display":"none"});
                $('#startTime_view').val("");
                $('#startTime').val("");
                $('#expirationTime_view').val("");
                $('#expirationTime').val("");
            });
            $('#vality-time').on('change',function () {
                var selectDate = $('#vality-time').val();
                hongbaoDetail.formatStyle(selectDate);
            });
        },
        formatStyle: function(selectDate){
            var date = new Date();
            var dateOld = date.getFullYear() + "-" ;
            if(date.getMonth()+1 < 10){dateOld += "0"}
            dateOld += date.getMonth()+1 + "-";
            if(date.getDate() < 10){dateOld += "0"}
            dateOld += date.getDate() + " ";
            if(date.getHours() < 10){dateOld += "0"}
            dateOld += date.getHours() + ":";
            if(date.getMinutes() < 10){dateOld += "0"}
            dateOld += date.getMinutes() + ":";
            if(date.getSeconds() < 10){dateOld += "0"}
            dateOld += date.getSeconds();

            var dateCurrent = date.getFullYear() + "-" ;
            if(date.getMonth()+1 < 10){dateCurrent += "0"}
            dateCurrent += date.getMonth()+1 + "-";
            if(date.getDate()+parseInt(selectDate) < 10){dateCurrent += "0"}
            dateCurrent += date.getDate() + parseInt(selectDate) +" ";
            if(date.getHours() < 10){dateCurrent += "0"}
            dateCurrent += date.getHours() + ":";
            if(date.getMinutes() < 10){dateCurrent += "0"}
            dateCurrent += date.getMinutes() + ":";
            if(date.getSeconds() < 10){dateCurrent += "0"}
            dateCurrent += date.getSeconds();
            $('#startTime_view').val(dateOld);
            $('#startTime').val(dateOld);
            $('#expirationTime_view').val(dateCurrent);
            $('#expirationTime').val(dateCurrent);
        },
        /**
         * 表单验证初始化
         */
        initValidator : function() {
            $('#hongbao_form').bootstrapValidator({
                message: '输入错误',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    couponName: {
                        message: '红包名称输入错误',
                        validators: {
                            notEmpty: {
                                message: '红包名称不能为空'
                            },
                            stringLength:{
                                message: '请输入1-12位的红包名称',
                                min : 1,
                                max : 12
                            }
                        }
                    },
                    startTime_view:{
                        message: '有效期开始时间输入错误',
                        validators: {
                            notEmpty: {
                                message: '有效期开始时间不能为空'
                            },
                            callback: {
                                message: '开始时间不能小于结束时间',
                                callback: function(value, validator) {
                                    var gmtCreateStart = + new Date($("#startTime").val()),
                                        gmtCreateEnd = + new Date($("#expirationTime").val());
                                    if(gmtCreateEnd < gmtCreateStart) {
                                        return false;
                                    }else{
                                        return true;
                                    }
                                }
                            }

                        }
                    },
                    expirationTime_view:{
                        message: '有效期开始时间输入错误',
                        validators: {
                            notEmpty: {
                                message: '有效期开始时间不能为空'
                            },
                            callback: {
                                message: '结束时间不能大于开始时间',
                                callback: function(value, validator) {
                                    var gmtCreateStart = + new Date($("#startTime").val()),
                                        gmtCreateEnd = + new Date($("#expirationTime").val());
                                    if(gmtCreateEnd < gmtCreateStart) {
                                        return false;
                                    }else{
                                        return true;
                                    }
                                }
                            }

                        }
                    },
                    reason:{
                        message: '发起原因输入错误',
                        validators: {
                            notEmpty: {
                                message: '发起原因不能为空'
                            },
                            stringLength:{
                                message: '请输入4-150位的发起原因',
                                min : 4,
                                max : 150
                            }

                        }
                    }
                }
            });
        },
        /**
         * 日期控件初始化
         */
        initDatetimepicker : function() {
            $('.form_datetime').datetimepicker({
                language:  'zh-CN',
                weekStart: 1,
                todayBtn:  1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 0,
                forceParse: 0
            });
        },
        /**
         * 发送红包
         */
        doSendHongbao : function(){
            $("#hongbao_form").data('bootstrapValidator').resetForm();
            $("#hongbao_form").data('bootstrapValidator').validate();
            var dataString,_data = {};
            $($("#hongbao_form").serializeArray()).each(function(){
                _data[this.name] = this.value;
            });
            // _data['couponName'] = "";




            _data.price = _data.price * 100;
            dataString = JSON.stringify(_data);
            if($("#hongbao_form").data('bootstrapValidator').isValid()) {
                CRM.request({
                    "type": 'POST',
                    "url": CRM.API.sendHongbao,
                    // "url":'http://daily.manage.51xianqu.com/crm/crm/market/sendHongbao',
                    "dataType": "json",

                    contentType: "application/json",
                    "data": dataString,
                    "success": function (req) {
                        if (req.status) {
                            $('#hongbao-detail-modal').modal('hide');
                            CRM.showDialog("红包发送成功！");
                        } else {
                            CRM.showDialog(req.message);
                        }
                    }
                });
            }
        }
    };


    return hongbaoDetail;

});